const state = {
	searchHistory1: uni.getStorageSync("searchHistory1") || [],  // 标品历史记录
	searchHistory2: uni.getStorageSync("searchHistory2") || [],  // 非标历史记录
	searchHistory3: uni.getStorageSync("searchHistory3") || []   // 全局搜索历史记录
};
const actions = {
	addToHistory({ commit }, valueObj) {
		commit('ADD_TO_HISTORY', valueObj);
	},
	/**
	 * @abstract 删除历史记录
	 * @augments type 1表示标品 2表示非标 3全局
	 */
	delSearchList({ commit }, type) {
		commit('CLEAR_HISTORY', type);
	}
};
const mutations = {
	ADD_TO_HISTORY(state, valueObj) {
		let { value, type } = valueObj;
		
		if(type == 1){
			// 去重
			const index = state.searchHistory1.findIndex(item => item === value);
			console.log(state.searchHistory1);
			if (index !== -1) {
				state.searchHistory1.splice(index, 1);
			}
			// 头插
			state.searchHistory1.unshift(value);
			// 控制长度
			if (state.searchHistory1.length > 10) {
				state.searchHistory1.pop();
			}
			uni.setStorageSync("searchHistory1", state.searchHistory1);
		} else if(type == 2){
			// 去重
			const index = state.searchHistory2.findIndex(item => item === value);
			if (index !== -1) {
				state.searchHistory2.splice(index, 1);
			}
			// 头插
			state.searchHistory2.unshift(value);
			// 控制长度
			if (state.searchHistory2.length > 10) {
				state.searchHistory2.pop();
			}
			uni.setStorageSync("searchHistory2", state.searchHistory2);
		}else {
			// 去重
			console.log('state.searchHistory3',state.searchHistory3);
			const index = state.searchHistory3.findIndex(item => item === value);
			if (index !== -1) {
				state.searchHistory3.splice(index, 1);
			}
			// 头插
			state.searchHistory3.unshift(value);
			// 控制长度
			if (state.searchHistory3.length > 10) {
				state.searchHistory3.pop();
			}
			uni.setStorageSync("searchHistory3", state.searchHistory3);
		}
	},
	CLEAR_HISTORY(state,type) {
		console.log('删除',type);
		if(type == 1){
			state.searchHistory1 = [];
			uni.setStorageSync("searchHistory1", state.searchHistory1);
			console.log(state.searchHistory1);
		}else if(type == 2){
			state.searchHistory2 = [];
			uni.setStorageSync("searchHistory2", state.searchHistory2);
		}else {
			state.searchHistory3 = [];
			uni.setStorageSync("searchHistory3", state.searchHistory3);
		}
		
	}
};
const getters = {
	searchHistory1: state => state.searchHistory1 || [],
	searchHistory2: state => state.searchHistory2 || [],
	searchHistory3: state => state.searchHistory3 || []
};
export default {
	namespaced: true,
	actions,
	mutations,
	state,
	getters,
}